String.prototype.trim = function()
{
  return this.replace(/(^\s*)|(\s*$)/g, "");
}
function checkPhone(userphone) 
{
  userphone = userphone.trim();
  var flag = false;
  var phoneError = document.getElementById("phoneError");
  var mod = /^[1][0-9][0-9]{9}$/;
  if (userphone == "")
    phoneError.innerHTML = "<br>手机号码不能为空";
  else if(userphone.length != 11)
    phoneError.innerHTML = "<br>输入的手机号码必须为11位";
  else if (!(mod.test(userphone)))
    phoneError.innerHTML = "<br>输入的手机号码格式错误";
  else
  {
    phoneError.innerHTML = "";
    flag = true;
  }
  return flag;
}
function clearPhoneError()
{
  var phoneError = document.getElementById("phoneError");
  phoneError.innerHTML = "";
}
function checkEmail(useremail) 
{
  useremail = useremail.trim();
  var flag = false;
  var emailError = document.getElementById("emailError");
  var mod = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  if (useremail == "") 
    emailError.innerHTML = "<br>电子邮箱不能为空";
  else if (!(mod.test(useremail)))
    emailError.innerHTML = "<br>输入的电子邮箱格式错误";
  else
  {
    emailError.innerHTML = "";
    flag = true;
  }
  return flag;
}
function clearEmailError() 
{
  var emailError = document.getElementById("emailError");
  emailError.innerHTML = "";
}
function checkUserpswd(userpswd)
{
  var pswdError = document.getElementById("pswdError");
  var flag = false;
  if (userpswd == "")
    pswdError.innerHTML = "<br>密码不能为空";
  else if (userpswd.length != 8)
    pswdError.innerHTML = "<br>密码长度必须为8位";
  else
  {
    pswdError.innerHTML = "";
    flag = true;
  }
  return flag;
}
function clearPswdError()
{
  var pswdError = document.getElementById("pswdError");
  pswdError.innerHTML = "";
}
function checkCode()
{
  var usercode = document.getElementById('usercode').value;
  var flag = false;
  if(usercode == "")
  {
    codeError.innerHTML = "<br>验证码不能为空";
  }
  else if (usercode.toLowerCase() == code.toLowerCase()) 
  {
    codeError.innerHTML = "";
    flag = true;
  }
  else
    codeError.innerHTML = "<br>输入的验证码错误";
  return flag;
}
function clearCodeError()
{
  var codeError = document.getElementById("codeError");
  codeError.innerHTML = "";
}
function checkAll() 
{
  var userpswd = document.getElementById("userpswd").value;
  var userphone = document.getElementById("userphone").value;
  var useremail = document.getElementById("useremail").value;
  var usercode = document.getElementById("usercode").value;
  if (checkUserpswd(userpswd) && checkPhone(userphone) && checkCode(usercode) && checkEmail(useremail))
  {
    document.button.submit();
    alert("注册成功");
  }
}